package guiParts;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import listener.ButtonActionListener;
import propertiesReader.PropertiesReader;
import xls.ReadXLS;
import fileIO.ReadTxtFiles;

public class EmailSendGui extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static boolean endversion = true;

	public JButton getBetreff() {
		return betreff;
	}

	public void setBetreff(JButton betreff) {
		this.betreff = betreff;
	}

	public JButton getTextButton() {
		return textButton;
	}

	public void setTextButton(JButton textButton) {
		this.textButton = textButton;
	}

	public JButton getAnhang() {
		return anhang;
	}

	public void setAnhang(JButton anhang) {
		this.anhang = anhang;
	}

	public JButton getXls() {
		return xls;
	}

	public void setXls(JButton xls) {
		this.xls = xls;
	}

	public JButton getStatus() {
		return status;
	}

	public void setStatus(JButton status) {
		this.status = status;
	}

	public JButton getSendedEmails() {
		return sendedEmails;
	}

	public void setSendedEmails(JButton sendedEmails) {
		this.sendedEmails = sendedEmails;
	}

	public JButton getSave() {
		return save;
	}

	public void setSave(JButton save) {
		this.save = save;
	}

	public JButton getReset() {
		return reset;
	}

	public void setReset(JButton reset) {
		this.reset = reset;
	}

	public JButton getSend() {
		return send;
	}

	public void setSend(JButton send) {
		this.send = send;
	}

	public JButton getStop() {
		return stop;
	}

	public void setStop(JButton stop) {
		this.stop = stop;
	}

	public String getTmpBetreff() {
		//System.out.println("Betreff wird geholt : " + tmpBetreff);
		return tmpBetreff;
	}

	public void setTmpBetreff(String tmpBetreff, String id) {
		//System.out.println("Betreff wird von " + id + " gesetzt : " + tmpBetreff);
		this.tmpBetreff = tmpBetreff;
	}

	public String getTmpContent() {
		return tmpContent;
	}

	public void setTmpContent(String tmpContent) {
		this.tmpContent = tmpContent;
	}

	public String getTmpAttachmentPath() {
		return tmpAttachmentPath;
	}

	public void setTmpAttachmentPath(String tmpAttachmentPath) {
		this.tmpAttachmentPath = tmpAttachmentPath;
	}

	public String getTmpXlsPath() {
		return tmpXlsPath;
	}

	public void setTmpXlsPath(String tmpXlsPath) {
		this.tmpXlsPath = tmpXlsPath;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	


	public static boolean isEndversion() {
		return endversion;
	}
	

	// Panel
	JPanel mainpaneL = new JPanel();

	JPanel leftPanel = new JPanel();
	JPanel innerLeftPanel = new JPanel(new GridLayout(6, 1, 5, 5));

	/*
	 * Contraints for centerpanel
	 */
	public GridBagConstraints constraints = new GridBagConstraints();

	// Progressbar
	public JProgressBar progressBar;

	// GridbagLayout
	public GridBagLayout gbl = new GridBagLayout();

	public JPanel centerPanel = new JPanel(new BorderLayout(5, 5));

	// The panel for the buttons at the bottom (send, reset...)
	public JPanel bottomPanel = new JPanel();


	public JButton getSendingStatus() {
		return sendingStatus;
	}

	public void setSendingStatus(JButton sendingStatus) {
		this.sendingStatus = sendingStatus;
	}
	
	
	/**
	 * Buttons
	 */
	
	/*
	 * Buttons left column
	 */

	private JButton betreff = new JButton("Betreff");
	private JButton textButton = new JButton("Inhalt");
	private JButton anhang = new JButton("Anhang");
	private JButton xls = new JButton("Xls-Datei");
	private JButton status = new JButton("Status");
	private JButton sendedEmails = new JButton("Email-Info");
	private JButton sendingStatus = new JButton("Fortschritt");

	/*
	 * Buttons bottom
	 */
	private JButton save = new JButton("Save");
	private JButton reset = new JButton("Reset");
	private JButton send = new JButton("Send");
	private JButton stop = new JButton("Stop");

	// temp vars
	private String tmpBetreff;
	private String tmpContent;
	private String tmpAttachmentPath;
	private String tmpXlsPath;
	private String savePath;
	private Set<String> emailAdresses;
	private Boolean checkBoxSet;
	

	public Boolean getCheckBoxSet() {
		return checkBoxSet;
	}

	public void setCheckBoxSet(Boolean checkBoxSet) {
		this.checkBoxSet = checkBoxSet;
	}


	// the actionlistener
	public ButtonActionListener mainListener;

	private List<JButton> buttons = new LinkedList<JButton>();

	public Color backGroundColorLeft = Color.DARK_GRAY;
	public Color backGroundColorCenter = Color.DARK_GRAY;

	private Border noBorder = BorderFactory
	.createBevelBorder(BevelBorder.RAISED);
	
	private PropertiesReader pReader;
	
	public EmailSendGui() throws IOException {

		pReader = new PropertiesReader(endversion);
		
		readTxtFiles();
		setGui();
		setActionListener();
		setFolder();
		
		
	}
	
	

	private void setFolder() {
		File persistenceFolder = new File("persistence");
		if(!persistenceFolder.exists()){
			persistenceFolder.mkdir();
		}
		
	}

	@SuppressWarnings("unused")
	private void printInfos() {
		printLine();
		System.out.println("Aktuelle Einstellungen: ");
		printLine();
		System.out.println("Betreff : " + tmpBetreff);
		printLine();
		System.out.println("Inhalt : " + tmpContent);
		printLine();
		System.out.println("Anhangspfad " + tmpAttachmentPath);
		printLine();
		System.out.println("XLSpfad     " + tmpXlsPath);
		printLine();
	}

	public void readTxtFiles() throws IOException {
		/*
		 * Read text-files + the attachmentpath and the xls-path
		 */
		ReadTxtFiles txtReader = new ReadTxtFiles();
		tmpBetreff = txtReader.readTxtFile(pReader.getProperty("subjectPath"));
		tmpBetreff = tmpBetreff.trim();
		setTmpBetreff(tmpBetreff, "readTxtFiles");
		tmpContent = txtReader.readTxtFile(pReader.getProperty("newsTextPath"));
		tmpContent = tmpContent.trim();
		setTmpContent(tmpContent);
		tmpAttachmentPath = pReader.getProperty("attachmentPath");
		tmpXlsPath = pReader.getProperty("xlsPath");
		
		checkBoxSet = Boolean.valueOf(pReader.getProperty("checkbox"));

		//printInfos();

	}

	private void printLine() {
		System.out.println("___________________________");
	}

	private void setActionListener() {
		ReadXLS xlsReader = ReadXLS.getInstance();

		emailAdresses = xlsReader.readXLS(pReader.getProperty("xlsPath"));
		boolean emailsWrong = xlsReader.isEmailsWrong();
		
		if(emailsWrong){
			System.err.println("Error(s) in XLS File");
			
		}
//		SendEmail sendMail = SendEmail.getInstance(new SendingPanel(), endversion, tmpBetreff,
//				tmpContent, tmpAttachmentPath, emailAdresses);
		mainListener = new ButtonActionListener(this, endversion);

		buttons.add(betreff);
		buttons.add(textButton);
		buttons.add(anhang);
		buttons.add(xls);
		buttons.add(status);
		buttons.add(sendedEmails);
		buttons.add(sendingStatus);
		buttons.add(save);
		buttons.add(reset);
		buttons.add(send);
		buttons.add(stop);

		// Action Listener
		for (JButton button : buttons) {
			button.addActionListener(mainListener);
			button.setBorder(noBorder);
			mainListener.registerButton(button);
			button.setFocusPainted(false);
		}

	}

	private void setGui() {

		// Main Frame
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setPreferredSize(new Dimension(650, 450));
		setSize(650, 450);
		setLayout(new BorderLayout());

		// Buttons

		textButton.setMinimumSize(new Dimension(100, 26));
		anhang.setPreferredSize(new Dimension(100, 26));
		xls.setPreferredSize(new Dimension(100, 26));
		status.setPreferredSize(new Dimension(100, 26));
		sendedEmails.setPreferredSize(new Dimension(100, 26));
		sendingStatus.setPreferredSize(new Dimension(100, 26));

		save.setPreferredSize(new Dimension(100, 26));
		send.setPreferredSize(new Dimension(100, 26));
		stop.setPreferredSize(new Dimension(100, 26));
		reset.setPreferredSize(new Dimension(100, 26));

		progressBar = new JProgressBar(0, 100);

		// Center
		constraints.insets = new Insets(5, 5, 5, 5);
		centerPanel.setMinimumSize(new Dimension(200, 200));
		// centerPanel.setBorder(border);
		centerPanel.setBackground(backGroundColorCenter);

		// Left Panel
		leftPanel.setLayout(new BorderLayout(5, 5));
		JPanel fill = new JPanel();
		fill.setBackground(backGroundColorLeft);
		innerLeftPanel.setLayout(new GridLayout(8, 1, 5, 5));
		innerLeftPanel.setBackground(backGroundColorLeft);
		innerLeftPanel.add(betreff);
		innerLeftPanel.add(textButton);
		innerLeftPanel.add(anhang);
		innerLeftPanel.add(xls);
		innerLeftPanel.add(status);
		innerLeftPanel.add(sendedEmails);
		sendingStatus.setEnabled(false);
		sendingStatus.setVisible(false);
		innerLeftPanel.add(sendingStatus);
		leftPanel.add(innerLeftPanel, BorderLayout.CENTER);
		fill = new JPanel();
		fill.setBackground(backGroundColorLeft);
		fill.setPreferredSize(new Dimension(5, 1));
		leftPanel.add(fill, BorderLayout.WEST);
		fill = new JPanel();
		fill.setBackground(backGroundColorLeft);
		fill.setPreferredSize(new Dimension(1, 5));
		leftPanel.add(fill, BorderLayout.NORTH);
		fill = new JPanel();
		fill.setBackground(backGroundColorLeft);
		fill.setPreferredSize(new Dimension(5, 1));
		leftPanel.add(fill, BorderLayout.EAST);
		leftPanel.setBackground(backGroundColorLeft);

		leftPanel.setBorder(BorderFactory.createBevelBorder(HEIGHT));
		leftPanel.setPreferredSize(new Dimension(120, 300));
		leftPanel.setMinimumSize(new Dimension(200, 300));

		// Bottom Panel
		bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		bottomPanel.setMinimumSize(new Dimension(200, 80));
		bottomPanel.add(save);
		bottomPanel.add(send);
		bottomPanel.add(reset);

		stop.setEnabled(false);
		bottomPanel.add(stop);
		bottomPanel.setBackground(Color.BLACK);

		// Mainpanel
		mainpaneL.setLayout(new BorderLayout(5, 5));
		fill = new JPanel();
		fill.setBackground(Color.BLACK);
		fill.setMinimumSize(new Dimension(650, 100));
		mainpaneL.add(fill, BorderLayout.NORTH);
		mainpaneL.add(leftPanel, BorderLayout.WEST);
		mainpaneL.add(centerPanel, BorderLayout.CENTER);
		mainpaneL.add(bottomPanel, BorderLayout.SOUTH);
		mainpaneL.setBackground(Color.white);
		fill = new JPanel();
		fill.setBackground(Color.WHITE);
		fill.setPreferredSize(new Dimension(1, 200));
		mainpaneL.add(fill, BorderLayout.EAST);
		add(mainpaneL, BorderLayout.CENTER);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) dim.getHeight() / 4;
		int y = (int) dim.getWidth() / 4;
		setLocation(y, x);
		setMinimumSize(new Dimension(450, 350));
		pack();
		repaint();
		setVisible(true);
	}

	public static void main(String[] args) throws IOException {
		System.out
				.println("####################################################");
		System.out
				.println("##########                                  ########");
		System.out
				.println("########## Javax.mail SMTP-AUTH Sender      ########");
		System.out
				.println("########## Version 2.2.8                    ########");
		System.out
				.println("##########                                  ########");
		System.out
				.println("########## Author:                          ########");
		System.out
				.println("########## Tim Kittelberger                 ########");
		System.out
				.println("##########                                  ########");
		System.out
				.println("########## Email:                           ########");
		System.out
				.println("########## tim.k.webservice@googlemail.com  ########");
		System.out
				.println("##########                                  ########");
		System.out
				.println("####################################################");

		new EmailSendGui();
	}

	public Set<String> getEmailAdresses() {
		return emailAdresses;
	}

	public void setEmailAdresses(Set<String> emailAdresses) {
		this.emailAdresses = emailAdresses;
	}
	
	

}
